//
//  Animation复习.swift
//  NaiCha
//
//  Created by 唐安坤 on 2024/8/29.
//

import SwiftUI

struct Animation__: View {
    
    @State var circleBg = Color.gray
    @State var starScalet:CGFloat = 1
    @State var angle:Double = 0
    var body: some View {
        VStack(spacing: 20) {
            //闪烁的星星
            ZStack{
                Circle()
                    .frame(width: 200, height: 200)
                    .foregroundColor(circleBg)
                Image(systemName: "star.fill")
                    .foregroundColor(.white)
                    .font(Font.system(size: 50))
                    .scaleEffect(starScalet)
            }.onTapGesture {
                withAnimation(Animation.linear(duration: 1).repeatForever()) {
                    circleBg = Color.orange
                    starScalet = 0.2
                }
            }
            
            //等待的跳动
            HStack{
                WaitCircle(num: 1)
                WaitCircle(num: 2)
                WaitCircle(num: 3)
                WaitCircle(num: 4)
                WaitCircle(num: 5)
            }
            
            Circle()
                .trim(from: /*@START_MENU_TOKEN@*/0.0/*@END_MENU_TOKEN@*/, to: 0.5)
                .stroke(style: StrokeStyle(lineWidth: 10,lineCap: .round))
                .rotation(Angle(degrees: angle))
                .frame(width: 200,height: 200).onAppear{
                    withAnimation(Animation.linear(duration: 1).repeatForever(autoreverses: false)) {
                        angle = 360
                    }
                }
            
            Circle().stroke(style: StrokeStyle(lineWidth: 30,lineCap: .round))
                .frame(width: 200,height: 200)
                .foregroundColor(.gray)
                .overlay {
                    Circle()
                        .trim(from: /*@START_MENU_TOKEN@*/0.0/*@END_MENU_TOKEN@*/,to: 0.5)
                        .stroke(style: StrokeStyle(lineWidth: 10, lineCap: .round, lineJoin: .bevel))
                        .rotation(Angle(degrees: angle))
                }
            
            
        }
    }
}

#Preview {
    Animation__()
}

struct WaitCircle: View {
    @State private var circleScalet:CGFloat = 1
    var num: CGFloat
    var body: some View {
        Circle().frame(width: 30, height: 30, alignment: .center).scaleEffect(circleScalet).foregroundColor(.green).onAppear{
            withAnimation (Animation.linear(duration: 0.5).repeatForever().delay(0.1*num)){
                circleScalet = 0.3
            }
        }
    }
}
